package com.example.democsv.service;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.json.JSONUtil;
import com.example.democsv.model.AreaStationData;

import java.io.BufferedReader;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author 赵恒
 * @date 2023/7/12
 */
public class AreaStationDataService {

    public List<AreaStationData> readCsv() {
        ClassLoader classLoader = AreaStationDataService.class.getClassLoader();
        InputStream stream = classLoader.getResourceAsStream("data/errorData1.csv");
        BufferedReader bufferedReader = IoUtil.getReader(stream, Charset.defaultCharset());
        CsvReader csvReader = CsvUtil.getReader();
        List<AreaStationData> areaStationDataList = csvReader.read(bufferedReader, AreaStationData.class);
        return  areaStationDataList;

    }

    public void writeCsv(List<AreaStationData> areaStationDataList) {
        CsvWriter csvWriter = CsvUtil.getWriter("D:\\temp\\csv\\station.csv", Charset.defaultCharset());
        csvWriter.writeBeans(areaStationDataList);
        System.out.println("导出成功");
    }
}
